package cqie.personal.frame.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import cqie.personal.frame.bo.AddModifyDepartmentBO;
import cqie.personal.frame.bo.GetDepartmentBO;
import cqie.personal.frame.entity.DepartmentInfo;
import cqie.personal.frame.exception.ValidationFailException;
import cqie.personal.frame.mapper.DepartmentInfoMapper;
import cqie.personal.frame.service.DepartmentInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yeeeeee
 * @since 2021-09-27
 */
@Service
public class DepartmentInfoServiceImpl extends ServiceImpl<DepartmentInfoMapper, DepartmentInfo> implements DepartmentInfoService {
    @Resource
    private DepartmentInfoMapper departmentInfoMapper;

    @Override
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_UNCOMMITTED)
    public synchronized boolean addDepartment(AddModifyDepartmentBO addBO) throws ValidationFailException {
        if(addBO == null){
            throw new ValidationFailException("伺服器錯誤，稍後重試");
        }
        if(addBO.getDepartmentName()==null){
            throw new ValidationFailException("名稱不能為空");
        }
        QueryWrapper<DepartmentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.last(" LIMIT 1 ");
        int id = departmentInfoMapper.selectOne(queryWrapper).getId();
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("department_name",addBO.getDepartmentName());
        if(departmentInfoMapper.selectCount(queryWrapper)>0){
            throw new ValidationFailException("名稱被使用");
        }
        DepartmentInfo departmentInfo = new DepartmentInfo();
        departmentInfo.setDepartmentName(addBO.getDepartmentName());

        DecimalFormat g1=new DecimalFormat("0000");
        String startZeroStr = g1.format(id);
        departmentInfo.setDepartmentCode(startZeroStr);
        departmentInfo.setDepartmentLevel(0);
        if(addBO.getDepartmentFather()!=null){
            queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id",addBO.getDepartmentFather());
            DepartmentInfo father = departmentInfoMapper.selectOne(queryWrapper);
            if(father == null){
                throw new ValidationFailException("父節點不可用");
            }
            departmentInfo.setDepartmentFather(addBO.getDepartmentFather());
            departmentInfo.setDepartmentFatherName(father.getDepartmentName());
            departmentInfo.setDepartmentLevel(father.getDepartmentLevel()+1);
            departmentInfo.setDepartmentCode(father.getDepartmentCode().concat(departmentInfo.getDepartmentCode()));
        }
        Date date = new Date();
        departmentInfo.setLastUpgradePerson(addBO.getUid());
        departmentInfo.setLastUpgradeTime(date);
        departmentInfo.setCreatePerson(addBO.getUid());
        departmentInfo.setCreateTime(date);

        return departmentInfoMapper.insert(departmentInfo)==1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_UNCOMMITTED)
    public synchronized boolean modifyDepartment(AddModifyDepartmentBO modifyBO) throws ValidationFailException {
        if(modifyBO==null){
            throw new ValidationFailException("伺服器錯誤，稍後重試");
        }
        if(modifyBO.getId()==null){
            throw new ValidationFailException("未能獲取修改節點");
        }
        if(modifyBO.getDepartmentName()==null){
            throw new ValidationFailException("名稱不能為空");
        }
        QueryWrapper<DepartmentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",modifyBO.getId());
        DepartmentInfo departmentInfo =departmentInfoMapper.selectOne(queryWrapper);
        if(departmentInfo == null){
            throw new ValidationFailException("操作節點不可用");
        }
        if(modifyBO.getDepartmentAbandon()!=null){
            departmentInfo.setDepartmentAbandon(modifyBO.getDepartmentAbandon()?1:0);
        }
        queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("id",modifyBO.getId());
        queryWrapper.eq("department_name",modifyBO.getDepartmentName());
        if(departmentInfoMapper.selectCount(queryWrapper)>0){
            DecimalFormat g1=new DecimalFormat("0000");
            String startZeroStr = g1.format(modifyBO.getId());
            departmentInfo.setDepartmentName(modifyBO.getDepartmentName()+"#"+startZeroStr);
        }else {
            departmentInfo.setDepartmentName(modifyBO.getDepartmentName());
        }
        departmentInfo.setDepartmentLevel(0);
        if(!modifyBO.getDepartmentFather().equals(departmentInfo.getDepartmentFather())){
            queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id",modifyBO.getDepartmentFather());
            DepartmentInfo father = departmentInfoMapper.selectOne(queryWrapper);
            if(father == null){
                throw new ValidationFailException("父節點不可用");
            }
            departmentInfo.setDepartmentFather(modifyBO.getDepartmentFather());
            departmentInfo.setDepartmentFatherName(father.getDepartmentName());
            departmentInfo.setDepartmentCode(father.getDepartmentCode().concat(departmentInfo.getDepartmentCode()));
            departmentInfo.setDepartmentLevel(father.getDepartmentLevel()+1);
        }
        UpdateWrapper<DepartmentInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",departmentInfo.getId());
        return departmentInfoMapper.update(departmentInfo,updateWrapper)==1;
    }

    @Override
    public List<DepartmentInfo> getDepartment(GetDepartmentBO departmentBO) throws ValidationFailException {
        if(departmentBO==null){
            throw new ValidationFailException("null");
        }
        QueryWrapper<DepartmentInfo> queryWrapper = new QueryWrapper<>();
        if(departmentBO.getId()!=null){
            queryWrapper.eq("department_father",departmentBO.getId());
        }
        if(departmentBO.getLevel()!=null){
            queryWrapper.eq("department_level",departmentBO.getLevel());
        }
        return  departmentInfoMapper.selectList(queryWrapper);
    }
}
